/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#if !defined(_DCLST_DOWNLOADER_H_)
#define _DCLST_DOWNLOADER_H_

#pragma once



#include "..\client\Singleton.h"
#include "..\client\Semaphore.h"
#include "..\client\Speaker.h"
#include "..\client\TimerManager.h"
#include "..\client\DirectoryListing.h"

class DCLSTDownloader : 
    public Singleton<DCLSTDownloader>, public Thread
{

public: 

	void initialize();
	void DownloadDCLSTContent(const tstring& DCLSTName);
    void shutdown();

private:
	friend class Singleton<DCLSTDownloader>;


	DCLSTDownloader(); 
	virtual ~DCLSTDownloader() throw();

	enum Tasks {
		START_DOWNLOAD,
		SHUTDOWN
	};

	
	struct TaskData { 
		TaskData(const tstring& dclstName) {  _dclstName = dclstName; }
		virtual ~TaskData() throw() { }
		tstring _dclstName;
	};

    	CriticalSection cs;
    	Semaphore taskSem;

	list<pair<Tasks, TaskData*> > m_tasks;

	int run();

	bool failed;
	void fail(const string& aError);	
	bool checkEvents();

	void addTask(Tasks task, TaskData* data) 
	{
	     m_tasks.push_back(make_pair(task, data)); 
		 taskSem.signal(); 
	}

	void startParseDCLSTThisThread(const tstring& DCLSTName);


	auto_ptr<DirectoryListing> dl;
};





#endif // _DCLST_DOWNLOADER_H_